package com.qjc.excel.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class DateUtils {

    public static void main(String[] args) {
        String afterDayDate = "2016-11-17";
        System.err.println(isLastThreeYears(afterDayDate));
    }

    /**
     * 判断日期是否是最近三年内
     *
     * @param dateString
     * @return
     */
    public static Boolean isLastThreeYears(String dateString) {
        String dateFrontOrAfterYear = getDateFrontOrAfterYear(formatDateToString(new Date(), "yyyy-MM-dd"), -3, "yyyy-MM-dd");
        if (dateString.compareTo(dateFrontOrAfterYear) > -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将Date类型的日期转换为指定格式的日期字符串
     *
     * @param date
     * @param pattern 常用格式：yyyy-MM-dd HH:mm:ss:SSS；yyyy年MM月dd日 HH时mm分ss秒SSS毫秒；yyyy/MM/dd HH/mm/ss/SSS
     * @return
     */
    public static String formatDateToString(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 字符串转为日期
     *
     * @param dateStr 字符串日期
     * @return 日期格式
     */
    public static Date formatStringToDate(String dateStr, String pattern) {

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将String类型的日期转换为指定格式的日期字符串
     *
     * @return String
     */
    public static String formatDateOfString(String dateStr, String pattern) {
        Date date = formatStringToDate(dateStr, pattern);
        String newDate = formatDateToString(date, pattern);
        return newDate;
    }

    /**
     * 得到某个日期几天后的时间 或几天前
     *
     * @param date date
     * @param day  day 为负数就是几天后，为正数就是几天前
     * @return String
     */
    public static String getDateFrontOrAfterDay(String date, int day, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String format1 = null;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(format.parse(date));
            now.set(Calendar.DATE, now.get(Calendar.DATE) + day);//+后 -前
            format1 = format.format(now.getTime());
        } catch (ParseException e) {
            log.error("String日期转为Date日期出现异常", e);
        }
        return format1;
    }

    /**
     * 获取某个日期几个月后/前的时间
     *
     * @param date
     * @param month 为负数就是几个月后，为正数就是几个月前
     * @return
     */
    public static String getDateFrontOrAfterMonth(String date, int month, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String format1 = null;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(format.parse(date));
            now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);//+后 -前
            format1 = format.format(now.getTime());
        } catch (ParseException e) {
            log.error("String日期转为Date日期出现异常", e);
        }
        return format1;
    }

    /**
     * 获取某个日期几年后/前的时间
     *
     * @param date
     * @param year 为负数就是几年后，为正数就是几年前
     * @return
     */
    public static String getDateFrontOrAfterYear(String date, int year, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String format1 = null;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(format.parse(date));
            now.set(Calendar.YEAR, now.get(Calendar.YEAR) + year);//+后 -前
            format1 = format.format(now.getTime());
        } catch (ParseException e) {
            log.error("String日期转为Date日期出现异常", e);
        }
        return format1;
    }

    /**
     * 获取某个日期几小时后/前的时间
     *
     * @param date
     * @param hours 为负数就是几小时后，为正数就是几小时前
     * @return
     */
    public static String getDateFrontOrAfterHour(String date, int hours, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String format1 = null;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(format.parse(date));
            now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + hours);//+后 -前
            format1 = format.format(now.getTime());
        } catch (ParseException e) {
            log.error("String日期转为Date日期出现异常", e);
        }
        return format1;
    }

    /**
     * 获取某个日期几分钟后/前的时间
     *
     * @param date    日期
     * @param minutes 为负数就是几分钟后，为正数就是几分钟前
     * @return
     */
    public static String getDateFrontOrAfterMinute(String date, int minutes, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String format1 = null;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(format.parse(date));
            now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minutes);//+后 -前
            format1 = format.format(now.getTime());
        } catch (ParseException e) {
            log.error("String日期转为Date日期出现异常", e);
        }
        return format1;
    }

    /**
     * 获取几天后的时间
     *
     * @param date
     * @param days
     * @return
     */
    public static String getAfterDayDate(String date, int days) {
        String nowDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = format.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            calendar.add(Calendar.DAY_OF_MONTH, days);
            nowDate = format.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate;
    }

    /**
     * 获取去年第一天日期
     *
     * @return
     */
    public static Date getLastYearFirstDay() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        year = year - 1;
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 给日期排序
     *
     * @param dateList
     * @return
     */
    public static List<Date> getDateListOfSort(List<Date> dateList) {
        dateList.sort(Comparator.reverseOrder());
        return dateList;
    }

    /**
     * 获取过去passYears年的数组
     *
     * @param passYears
     * @return
     */
    public static List<String> getYearList(Integer passYears) {
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        //过去一年
        c.setTime(new Date());
        passYears--;
        c.add(Calendar.YEAR, -passYears);
        Date y = c.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        String passyear = format.format(y);
        String nowyear = format.format(new Date());
        int passyearint = Integer.parseInt(passyear);
        int nowyearint = Integer.parseInt(nowyear);
        while (passyearint <= nowyearint) {
            list.add(String.valueOf(passyearint));
            passyearint++;
        }
        return list;
    }

    /**
     * String日期转成Date日期
     *
     * @return String
     */
    public static Date stringToDate(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException pe) {
            log.info(pe.getMessage());
        }
        return date;
    }

    /**
     * 返回日期
     *
     * @return String
     */
    public static String getStringDate(String dateStr, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String gbdate = null;
        try {
            Date date = format.parse(dateStr);
            gbdate = format.format(date);
        } catch (ParseException pe) {
            log.info(pe.getMessage());
            log.error("日期转换异常", pe);
        }
        return gbdate;
    }


    /**
     * 获取几个月之后的时间
     *
     * @param date
     * @param dateType
     * @param month
     * @return
     */
    public static String getAfterMonthesDate(String date, String dateType, int month) {
        String nowDate = null;
        SimpleDateFormat format = new SimpleDateFormat(dateType);
        try {
            Date parse = format.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            calendar.add(Calendar.MONTH, month);
            nowDate = format.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate;
    }

    /**
     * 将Date类型的日期转换为指定格式的日期字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date getCurrDay(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateDay = format.format(date);
        try {
            return format.parse(dateDay);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /**
     * 描述：间隔天数
     *
     * @param date1
     * @param date2
     * @return 天数
     */
    public static int getDateDiff(Date date1, Date date2) {
        if ((date1 == null) || (date2 == null)) {
            return 0;
        }
        long time1 = date1.getTime();
        long time2 = date2.getTime();

        long diff = time1 - time2;

        Long longValue = new Long(diff / 86400000L);
        return longValue.intValue();
    }

    /**
     * 两个时间之间的天数:date1比date2多几天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getDays(Date date1, Date date2) {
        String pattern = "yyyy-MM-dd";
        Date date = null;
        Date mydate = null;
        try {
//            date = myFormatter.parse(date1);
            date = formatDate(date1, pattern);
//            mydate = myFormatter.parse(date2);
            mydate = formatDate(date2, pattern);
        } catch (Exception e) {
        }
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        return day + 1;//当天也算是一天
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static Date formatDate(Date date, String pattern) {
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat(pattern);
            String format = myFormatter.format(date);
            Date parse = myFormatter.parse(format);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

}
